﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace 计算区间数据
{
    public partial class Forms1 : Form
    {
        public Forms1()
        {
            InitializeComponent();

            //初始化表格
            //根据Header和所有单元格的内容自动调整行的高度
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            //设置内容对齐方式
            dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //设置标题头列宽
            dataGridView1.RowHeadersWidth = 15;
            //设置列宽
            dataGridView1.Columns[0].Width = 139;
            dataGridView1.Columns[1].Width = 139;
            dataGridView1.Columns[2].Width = 139;

            //添加事件
            dataGridView1.KeyDown += dataGridView1_KeyDown;
            dataGridView1.MouseClick += dataGridView1_MouseClick;
            //dataGridView1.CellMouseClick += dataGridView1_CellMouseClick;

        }

        //点击右键删除对应行
        private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
        {
            //如果鼠标点击右键且选中行大于0 
            if (e.Button == MouseButtons.Right)
            {
                if (dataGridView1.Rows.Count == 0)
                {
                    MessageBox.Show("没有可以删除的行了！");
                    return;
                }
                //删除选中的最后一行
                dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
            }
        }
        //点击回车添加新行
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //添加新的一行
                string[] data = { "", "", "" };

                dataGridView1.Rows.Add(data);
            }
        }
        //点击按钮输出文件
        private void button_Click(object sender, EventArgs e)
        {
            //确定长度值
            int length = 400;
            try
            {
                length = int.Parse(textLength.Text.Trim());
            }
            catch (FormatException)
            {
                MessageBox.Show("计算长度格式有误！");
            }
            //生成区间
            List<Interval> intervals = new List<Interval>();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                try
                {
                    string s1, s2, s3;
                    s1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    s2 = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    s3 = dataGridView1.Rows[i].Cells[2].Value.ToString();
                    //格式校验
                    if (s1 == "")
                    {
                        if (intervals[intervals.Count - 1].Start < int.Parse(dataGridView1.Rows[i - 1].Cells[1].Value.ToString()))
                        {
                            throw new Exception("最后一区间跨度不足");
                        }
                        return;
                    }
                    else if (!Regex.IsMatch(s1, "^[A-Z]{1}$"))
                    {
                        throw new Exception("区间前缀命名不规范");
                    }
                    else if (s2 == null || s3 == null || s2.Length == 0 || s3.Length == 0)
                    {
                        throw new Exception("数据不可为空");
                    }

                    Interval interval = new Interval(s1,
                        int.Parse(s2),
                        int.Parse(s3));
                    if (i > 0 && interval.Start < int.Parse(dataGridView1.Rows[i - 1].Cells[2].Value.ToString()))
                    {
                        throw new Exception("前后区间应连贯");
                    }
                    intervals.Add(interval);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            //生成字符串组
            List<string> data = new List<string>();
            //字符串的添加
            int x = 0; 

            for (int j = 0; j < intervals.Count; j++)
            {
                int loselength = 0;
                //若为尾部区间
                if (j == (intervals.Count - 1))
                {
                    string s = data[data.Count - 1];
                    if (s.Count(c => c == ',') == 2)
                    {
                        s += intervals[j].Name + "," + x + "-无";
                    }
                    else
                    {
                        s = s.Substring(0, s.Length - 1);
                        int location = s.IndexOf(',');
                        location = s.IndexOf(',', location + 1);
                        s = s.Insert(location + 1, intervals[j].Name + "," + x + "-");
                    }
                    data[data.Count - 1] = s;

                    int count2 = (intervals[j].End - x) / length;
                    for (int i = 0; i < count2; i++)
                    {
                        string d1 = intervals[j].Name + "," + x + "," + intervals[j].Name + "," + (x + length) + "-无";
                        x += length;
                        data.Add(d1);
                    }
                    string end = intervals[j].Name + "," + x + "," + intervals[j].Name + "," + intervals[j].End + "-无";
                    data.Add(end);

                }
                else if (intervals[j].End < x)
                {
                    //当跨越位置超出所在区间时
                    data[data.Count-1] += intervals[j].Name + "," + intervals[j].Start + "," + intervals[j].Name + "," + intervals[j].End +",";
                }
                //当跨越位置不超出所在区间时
                else
                {
                    if(j==0){
                        int count = (intervals[j].End - x) / length;
                        for (int i = 0; i < count; i++)
                        {
                            string d1 = intervals[j].Name + "," + x + "," + intervals[j].Name + "," + (x + length) + "-无";
                            x += length;
                            data.Add(d1);
                        }
                        //计算剩余长度
                        loselength = intervals[j].End - x;
                        //准备语句前半部分
                        string d2 = intervals[j].Name + "," + x + ",";
                        //计算新的落脚点
                        x = intervals[j + 1].Start + length - loselength;
                        //添加语句前半部分
                        data.Add(d2);
                    }
                    else
                    {
                        //字符串拼接
                        string s = data[data.Count - 1];
                        if (s.Count(c => c== ',')==2)
                        {
                            s += intervals[j].Name + "," + x + "-无";
                        }
                        else
                        {
                            s = s.Substring(0,s.Length - 1);
                            int location = s.IndexOf(',');
                            location = s.IndexOf(',', location + 1);
                            s = s.Insert(location + 1, intervals[j].Name + "," + x + "-");
                        }
                        data[data.Count - 1] = s;

                        int count = (intervals[j].End - x) / length;
                        for (int i = 0; i < count; i++)
                        {
                            string d1 = intervals[j].Name + "," + x + "," + intervals[j].Name + "," + (x + length) + "-无";
                            x += length;
                            data.Add(d1);
                        }
                        loselength = intervals[j].End - x;
                        string d2 = intervals[j].Name + "," + x + ",";
                        x = intervals[j + 1].Start + length - loselength;
                        data.Add(d2);
                    }

                }

            }

            //输出文件
            using (StreamWriter streamWriter = new StreamWriter("D:\\区间数据.txt"))
            {
                foreach (string s in data)
                {
                    streamWriter.WriteLine(s);
                }
            }
        }


    }
}
